package cn.itrip.controller;

import cn.itrip.beans.dto.Dto;
import cn.itrip.beans.pojo.ItripComment;
import cn.itrip.beans.pojo.ItripHotelOrder;
import cn.itrip.beans.pojo.ItripUser;
import cn.itrip.beans.vo.comment.ItripListCommentVO;
import cn.itrip.beans.vo.comment.ItripScoreCommentVO;
import cn.itrip.beans.vo.order.*;
import cn.itrip.common.*;
import cn.itrip.service.hotel.HotelService;
import cn.itrip.service.hotel.WeekUtils;
import cn.itrip.service.order.HotelOrdeService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Controller
@Api(value = "API", basePath = "/http://api.itrap.com/api")
@RequestMapping(value = "/api/hotelorder")
@Slf4j
public class HotelOrderController {

    @Resource
    private HotelOrdeService hotelOrdeService;

    @Resource
    private ValidationToken validationToken;

    @Resource
    private HotelService hotelService;

    @RequestMapping(value = "/getpreorderinfo", produces = "application/json", method = RequestMethod.POST)
    @ResponseBody
    public Dto<PreAddOrderVO> getPreorderInfo(@RequestBody PreAddOrderVO preAddOrderVO, HttpServletRequest request, HttpServletResponse response) {

        List<PreAddOrderVO> itripScoreCommentVOS = new ArrayList<>();
//        String tokenString = CookieUtil.readLoginToken(request);
//        log.info("tokenString>>>>>>>>>>>>>" + tokenString);
//        log.debug("token name is from header : " + tokenString);
//        ItripUser currentUser = validationToken.getCurrentUser(tokenString);
        try {
//            if (null != currentUser) {
                Map param = new HashMap();
                param.put("checkInDate", preAddOrderVO.getCheckInDate());
                param.put("checkOutDate", preAddOrderVO.getCheckOutDate());
                param.put("count", preAddOrderVO.getCount());
                param.put("hotelId", preAddOrderVO.getHotelId());
                param.put("roomId", preAddOrderVO.getRoomId());
                List<ItripHotelOrder> itripHotelOrders = hotelOrdeService.getHotelOrderByMap(param);
                if (EmptyUtils.isNotEmpty(itripHotelOrders)) {
                    itripScoreCommentVOS = itripHotelOrders.stream()
                            .map(e -> new PreAddOrderVO(e.getHotelId(),e.getRoomId(),e.getCheckInDate(),e.getCheckOutDate(),e.getCount(),e.getHotelName(),e.getPayAmount()))
                            .collect(Collectors.toList());
                }
                Map param1 = new HashMap();
                param1.put("hotelId", preAddOrderVO.getHotelId());
                param1.put("roomId", preAddOrderVO.getRoomId());
                Integer score = hotelOrdeService.getHotelOrderStore(param1);
                for (PreAddOrderVO itripScoreCommentVO : itripScoreCommentVOS) {
                    itripScoreCommentVO.setStore(score);
                }
//            }
        } catch (Exception e) {
            DtoUtil.returnFail("系统异常", "10205");
            e.printStackTrace();
        }
        return DtoUtil.returnSuccess("获取酒店评分成功",itripScoreCommentVOS);
    }
    @RequestMapping(value = "/getpersonalorderlist", produces = "application/json", method = RequestMethod.POST)
    @ResponseBody
    public Dto<Page<ItripListHotelOrderVO>> getPersonalOrderList(@RequestBody ItripSearchOrderVO itripSearchOrderVO,HttpServletRequest request) {
//        String tokenString = CookieUtil.readLoginToken(request);
//        log.info("tokenString>>>>>>>>>>>>>" + tokenString);
//        log.debug("token name is from header : " + tokenString);
//        ItripUser currentUser = validationToken.getCurrentUser(tokenString);
        List<ItripListHotelOrderVO> itripListHotelOrderVOS = new ArrayList<>();
        List<ItripHotelOrder> itripHotelOrders = new ArrayList<>();
        Page page = new Page<>();
        try {
//            if (null != currentUser) {
                Map param = new HashMap();
                if (itripSearchOrderVO.getOrderStatus()!=-1){
                    param.put("orderStatus", itripSearchOrderVO.getOrderStatus());
                }
                if (itripSearchOrderVO.getOrderType()!=-1){
                    param.put("orderType", itripSearchOrderVO.getOrderType());
                }
                PageInfo pageInfo = hotelOrdeService.getHotelOrederListByPage(param, itripSearchOrderVO.getPageNo(), itripSearchOrderVO.getPageSize());
                itripHotelOrders = pageInfo.getList();
                if (EmptyUtils.isNotEmpty(itripHotelOrders)) {
                    itripListHotelOrderVOS = itripHotelOrders.stream()
                            .map(e -> new ItripListHotelOrderVO(e.getId(), e.getHotelId(), e.getHotelName(), e.getOrderNo(), e.getOrderType(),
                                    e.getLinkUserName(), e.getCreationDate(), e.getCheckInDate(), e.getPayAmount(), e.getOrderStatus()))
                            .collect(Collectors.toList());
                }
                page.setBeginPos(pageInfo.getFirstPage());
                page.setCurPage(pageInfo.getPageNum());
                page.setTotal((int) pageInfo.getTotal());
                page.setPageSize(pageInfo.getPageSize());
                page.setPageCount(pageInfo.getSize());
                page.setRows(itripListHotelOrderVOS);
//            }else{
//                return DtoUtil.returnFail("您需要登录", "10205");
//            }
        } catch (Exception e) {
            DtoUtil.returnFail("系统异常", "10205");
            e.printStackTrace();
        }
        return DtoUtil.returnSuccess("获取个人酒店订单列表成功",page);
    }
    @RequestMapping(value = "/getpersonalorderinfo/{orderId}", produces = "application/json", method = RequestMethod.GET)
    @ResponseBody
    public Dto<ItripPersonalHotelOrderVO> getPreorderInfo(@PathVariable Integer orderId) {

        ItripPersonalHotelOrderVO itripPersonalHotelOrderVO = new ItripPersonalHotelOrderVO();
//        String tokenString = CookieUtil.readLoginToken(request);
//        log.info("tokenString>>>>>>>>>>>>>" + tokenString);
//        log.debug("token name is from header : " + tokenString);
//        ItripUser currentUser = validationToken.getCurrentUser(tokenString);
        try {
//            if (null != currentUser) {
            Map param = new HashMap();
            param.put("id", orderId);
            List<ItripHotelOrder> itripHotelOrders = hotelOrdeService.getHotelOrderByMap(param);
            if (EmptyUtils.isNotEmpty(itripHotelOrders)) {
                for (ItripHotelOrder itripHotelOrder : itripHotelOrders) {
                    itripPersonalHotelOrderVO.setId(itripHotelOrder.getId());
                    itripPersonalHotelOrderVO.setBookType(itripHotelOrder.getBookType());
                    itripPersonalHotelOrderVO.setCreationDate(itripHotelOrder.getCreationDate());
                    itripPersonalHotelOrderVO.setNoticePhone(itripHotelOrder.getNoticePhone());
                    itripPersonalHotelOrderVO.setOrderNo(itripHotelOrder.getOrderNo());
                    itripPersonalHotelOrderVO.setOrderStatus(itripHotelOrder.getOrderStatus());
                    itripPersonalHotelOrderVO.setPayAmount(itripHotelOrder.getPayAmount());
                    itripPersonalHotelOrderVO.setPayType(itripHotelOrder.getPayType());
                    itripPersonalHotelOrderVO.setRoomPayType(1);
                }
            }
            /**
             * 订单流程:
             * 1、待付款、待评价（已消费）、未出行（支付成功）
             * 流程: 已提交-->待支付-->支付成功-->已入住-->已点评
             * 2、已取消
             * 流程: 已提交-->待支付-->已取消
             */
            if (itripPersonalHotelOrderVO.getOrderStatus()==0){
                itripPersonalHotelOrderVO.setProcessNode("2");
                itripPersonalHotelOrderVO.setOrderProcess("已提交-->待支付");
            }else if(itripPersonalHotelOrderVO.getOrderStatus()==1){
                itripPersonalHotelOrderVO.setProcessNode("3");
                itripPersonalHotelOrderVO.setOrderProcess("已提交-->待支付-->已取消");
            }else if(itripPersonalHotelOrderVO.getOrderStatus()==2){
                itripPersonalHotelOrderVO.setProcessNode("3");
                itripPersonalHotelOrderVO.setOrderProcess("已提交-->待支付-->支付成功");
            }else{
                itripPersonalHotelOrderVO.setProcessNode("4");
                itripPersonalHotelOrderVO.setOrderProcess("已提交-->待支付-->支付成功-->已入住-->已点评");
            }
//            }
        } catch (Exception e) {
            DtoUtil.returnFail("系统异常", "10205");
            e.printStackTrace();
        }
        return DtoUtil.returnSuccess("获取酒店评分成功",itripPersonalHotelOrderVO);
    }
    @RequestMapping(value = "/getpersonalorderroominfo/{orderId}", produces = "application/json", method = RequestMethod.GET)
    @ResponseBody
    public Dto<ItripPersonalOrderRoomVO> getPersonalOrderRoominfo(@PathVariable Integer orderId) {

        ItripPersonalOrderRoomVO itripPersonalOrderRoomVO = new ItripPersonalOrderRoomVO();
//        String tokenString = CookieUtil.readLoginToken(request);
//        log.info("tokenString>>>>>>>>>>>>>" + tokenString);
//        log.debug("token name is from header : " + tokenString);
//        ItripUser currentUser = validationToken.getCurrentUser(tokenString);
        try {
//            if (null != currentUser) {
            itripPersonalOrderRoomVO = hotelService.getItripPersonalOrderRoom(orderId);
            itripPersonalOrderRoomVO.setCheckInWeek(WeekUtils.getWeekOfDate(itripPersonalOrderRoomVO.getCheckInDate()));
            itripPersonalOrderRoomVO.setCheckOutWeek(WeekUtils.getWeekOfDate(itripPersonalOrderRoomVO.getCheckOutDate()));
//        }
        } catch (Exception e) {
            DtoUtil.returnFail("系统异常", "10205");
            e.printStackTrace();
        }
        return DtoUtil.returnSuccess("获取酒店评分成功",itripPersonalOrderRoomVO);
    }
}
